<template>
	<view class="page-index">
		<view class="news-box" @click="gosystem()" v-if="system">
			<uni-badge size="small" :text="unread" absolute="rightTop" type="error" :offset="[5, 5]" style="border:none">
				<image src="/static/news/notice.png" mode=""></image>
			</uni-badge>
			<view class="value">
				<view class="title">
					系统通知
				</view>
				<view class="new">
					{{ system.txt }}
				</view>
			</view>
			<view class="date">
				{{ system.create_time }}
			</view>
		</view>
		<view class="news-box" @click="golook()" v-if="look">
			<image src="/static/news/see.png" mode=""></image>
			<view class="value">
				<view class="title">
					查看了您
				</view>
				<view class="new"> {{ look.company.name }}查看了您 </view>
			</view>
			<view class="date">
				{{ look.create_time }}
			</view>
		</view>
		<view class="news-box" v-for="(item, index) in conversations" :key="index" v-if="conversations.length > 0">
			<uni-badge size="small" :text="item.unread" absolute="rightTop" type="error" :offset="[5, 5]" style="border:none">
				<image :src="item.data.avatar" mode="" @click="goInfo(item.userId)"></image>
			</uni-badge>
			<view class="value" @click="jump(item)">
				<view class="title">
					{{ item.data.nickname }}
				</view>
				<view class="new">
					<text v-if="item.lastMessage.type == 'positions'">需求消息</text>
					<text v-if="item.lastMessage.type == 'resumes'">接单消息</text>
					<text v-if="item.lastMessage.type == 'image'">图片消息</text>
					<text v-if="item.lastMessage.type == 'custom'">打招呼</text>
					<text v-else>{{ item.lastMessage.payload.text }}</text>
				</view>
			</view>
			<view class="date">
				{{ beautify_time(item.lastMessage.timestamp) ? beautify_time(item.lastMessage.timestamp) : '刚刚' }}
			</view>
		</view>
	</view>
</template>

<script>
	import * as news from '@/api/news.js';
	export default {
		data() {
			return {
				system: '',
				look: '',
				unread: 0,
				main: '',
				noticeStatus: true
			};
		},
		onShow() {
			this.get_system();
			if (uni.getStorageSync('token')) {
				this.resumeBrowseList();
				this.getUnreadMessageNum();
			}
			this.main = uni.getAppAuthorizeSetting().notificationAuthorized
		},

		computed: {
			conversations() {
				return this.$store.state.conversations;
			},
		},
		watch: {
			conversations(newval) {
				this.conversations = newval;
			},
			conversations: {
				handler(nval) {},
				deep: true,
				immediate: true,
			},
		},
		methods: {
			jump(item) {
				this.$tools.navigate(`/pages/news/chat/index?userId=${item.userId}`);
			},
			//系统通知
			get_system() {
				news.get_system().then((res) => {
					this.system = res.data.data[0];
				});
			},
			getUnreadMessageNum() {
				news.getUnreadMessageNum().then((res) => {
					this.unread = res.data;
				});
			},
			gosystem() {
				this.$tools.navigate(`/pages/news/system/index`);
			},
			golook() {
				this.$tools.navigate(`/pages/news/lookme/index`);
			},
			//查看了我
			resumeBrowseList() {
				news.resumeBrowseList({
					page: 1,
					pages: 1
				}).then((res) => {
					this.look = res.data.data[0];
				});
			},
			//去详细
			goInfo(id) {
				if (uni.getStorageSync('Type') == 1) {
					this.$tools.navigate(`/pages/home/resumeInfo/index?resume_id=${id.replace('p', '')}`);
				} else {
					this.$tools.navigate(`/pages/home/companyInfo/index?member_id=${id.replace('p', '')}`);
				}
			},
			// 处理时间
			beautify_time(timestamp) {
				var mistiming = (Math.round(Date.now()) - timestamp) / 1000;
				// console.log(timestamp,Date.now(),mistiming)
				// console.log(new.Data(2169076))
				var postfix = mistiming > 0 ? '前' : '后';
				mistiming = Math.abs(mistiming);
				var arrr = ['年', '个月', '星期', '天', '小时', '分钟', '秒'];
				var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];

				if (mistiming > 0) {
					for (var i = 0; i < 7; i++) {
						var inm = Math.floor(mistiming / arrn[i]);
						if (inm != 0) {
							if (i == 3 && inm < 24) {
								// return this.formatDate(timestamp)
							}
							return inm + arrr[i] + postfix;
						}
					}
				}
			},
		},
	};
</script>

<style scoped lang="scss">
	.page-index {
		padding: 0 30rpx;

		.news-box {
			margin-top: 30rpx;
			display: flex;
			justify-content: space-between;
			// align-items: center;

			image {
				width: 110rpx;
				height: 110rpx;
				flex-shrink: 0;
				border-radius: 50%;
			}

			.value {
				width: 100%;
				white-space: nowrap;
				/*超出的空白区域不换行*/
				overflow: hidden;
				/*超出隐藏*/
				text-overflow: ellipsis;
				/*文本超出显示省略号*/
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin-left: 19rpx;
				padding: 10rpx;

				.title {
					font-size: 30rpx;
					font-weight: 500;
					color: #666666;
					margin-bottom: 5rpx;
				}

				.new {
					font-size: 24rpx;
					font-weight: 400;
					color: #b5b5b5;
					margin-top: 5rpx;
					overflow: hidden;

					white-space: nowrap;

					text-overflow: ellipsis;

					-o-text-overflow: ellipsis;
				}
			}

			.date {
				flex-shrink: 0;
				font-size: 22rpx;
				font-weight: 400;
				color: #999999;
				padding-top: 10rpx;
			}
		}
	}
</style>
